package com.tc.data.spring.validator;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 *  @author zhixin02.liu
 */
public class TransactionAnnotationPropagationValidator implements TransactionAnnotationValidator {
	private Set<String> whitelist = new HashSet<String>();
	public TransactionAnnotationPropagationValidator() {}

	public TransactionAnnotationPropagationValidator(Collection<String> whitelist) {
		this.whitelist.addAll(whitelist);
	}

	/**
	 *  Check whether @Transactional's propagation is equals to Propagation.REQUIRED.
	 *  The abuse of Propagation's level are dangerous.
	 *  So we log a warning to notify you aware this thing. 
	 */
	@Override
	public void validateTransactionAnnotation(Transactional transactional, String name) {
		if (!whitelist.contains(name) && !Propagation.REQUIRED.equals(transactional.propagation())) {
			throw new RuntimeException("Your code exists Method or Class with @Transactional annotation that not use Propagation.REQUIRED. " +
										"Please Check Method : " + name + ". " +
										"Please don't do this unless you really know what you do. " +
										"Visit http://wiki.corp.vipshop.com/pages/viewpage.action?pageId=225576984 for more details. ");
		}
	}
}
